﻿Imports System.Data.SqlClient
Public Class HelperClass

    Public Enum EditMode
        Load = 0
        Edit = 1
        Add = 2
    End Enum

    Public Enum AssetTreeLevel
        defaultLevel = 0
        mainLevel = 1
        classLevel = 2
        categoryLevel = 3
        assetLevel = 4
    End Enum

    Public Shared Sub DisablePasswordBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As PasswordBox = TryCast(child, PasswordBox)
            If box IsNot Nothing Then
                box.Foreground = Brushes.Red
                box.IsEnabled = False
            Else
                DisablePasswordBox(child)
            End If
        Next
    End Sub

    Public Shared Sub EnablePasswordBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As PasswordBox = TryCast(child, PasswordBox)
            If box IsNot Nothing Then
                box.Foreground = Brushes.Black
                box.IsEnabled = True
            Else
                EnablePasswordBox(child)
            End If
        Next
    End Sub

    Public Shared Sub ClearTextBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As TextBox = TryCast(child, TextBox)
            If box IsNot Nothing Then
                If box.Text = "" Then
                    box.Text = "a"
                End If
                box.Text = Nothing
            Else
                ClearTextBox(child)
            End If
        Next
    End Sub

    Public Shared Sub ClearCheckBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As CheckBox = TryCast(child, CheckBox)
            If box IsNot Nothing Then
                box.IsChecked = False
            Else
                ClearCheckBox(child)
            End If
        Next
    End Sub

    Public Shared Sub ClearPasswordBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As PasswordBox = TryCast(child, PasswordBox)
            If box IsNot Nothing Then
                box.Password = Nothing
            Else
                ClearPasswordBox(child)
            End If
        Next
    End Sub

    Public Shared Sub ClearComboBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As ComboBox = TryCast(child, ComboBox)
            If box IsNot Nothing Then
                box.Text = ""
                box.SelectedIndex = -1
            Else
                ClearComboBox(child)
            End If
        Next
    End Sub

    
    Public Shared Sub DisableTextBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As TextBox = TryCast(child, TextBox)
            If box IsNot Nothing Then
                box.Foreground = Brushes.Red
                box.IsEnabled = False
            Else
                DisableTextBox(child)
            End If
        Next
    End Sub

    Public Shared Sub EnableTextBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As TextBox = TryCast(child, TextBox)
            If box IsNot Nothing Then
                box.Foreground = Brushes.Black
                box.IsEnabled = True
            Else
                EnableTextBox(child)
            End If
        Next
    End Sub

    Public Shared Sub EnableCheckBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As CheckBox = TryCast(child, CheckBox)
            If box IsNot Nothing Then
                box.Foreground = Brushes.Black
                box.IsEnabled = True
            Else
                EnableCheckBox(child)
            End If
        Next
    End Sub
    Public Shared Sub DisableCheckBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As CheckBox = TryCast(child, CheckBox)
            If box IsNot Nothing Then
                box.Foreground = Brushes.Red
                box.IsEnabled = False
            Else
                DisableCheckBox(child)
            End If
        Next
    End Sub

    Public Shared Sub DisableComboBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As ComboBox = TryCast(child, ComboBox)
            If box IsNot Nothing Then
                box.Foreground = Brushes.Red
                box.IsEnabled = False
            Else
                DisableComboBox(child)
            End If
        Next
    End Sub

    Public Shared Sub EnableComboBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As ComboBox = TryCast(child, ComboBox)
            If box IsNot Nothing Then
                box.Foreground = Brushes.Black
                box.IsEnabled = True
            Else
                EnableComboBox(child)
            End If
        Next
    End Sub

    Public Shared Sub DisableGroupBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As GroupBox = TryCast(child, GroupBox)
            If box IsNot Nothing Then
                box.Foreground = Brushes.Red
                box.IsEnabled = False
            Else
                DisableGroupBox(child)
            End If
        Next
    End Sub

    Public Shared Sub EnableGroupBox(ByVal parent As DependencyObject)
        For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(parent) - 1
            Dim child As DependencyObject = VisualTreeHelper.GetChild(parent, i)
            Dim box As GroupBox = TryCast(child, GroupBox)
            If box IsNot Nothing Then
                box.Foreground = Brushes.Black
                box.IsEnabled = True
            Else
                EnableGroupBox(child)
            End If
        Next
    End Sub

    Public Shared Sub CheckForSQLErrors(ByVal ex As Exception)
        If TypeOf ex Is SqlException Then
            Dim ext As SqlException = DirectCast(ex, SqlException)
            If ext.Number = 547 Then
                System.Windows.MessageBox.Show("Record cannot be deleted or changed " & "as it is being used somewhere else", "Info", MessageBoxButton.OK, MessageBoxImage.Hand)

            ElseIf ext.Number = 2627 Then
                System.Windows.MessageBox.Show("Record cannot be saved, as another " & "record with this key already exists", "Info", MessageBoxButton.OK, MessageBoxImage.Hand)
            Else
                System.Windows.MessageBox.Show(ex.Message.ToString(), "Info", MessageBoxButton.OK, MessageBoxImage.Hand)
            End If
        Else
            System.Windows.MessageBox.Show("System Error :" & ex.Message.ToString(), "Info", MessageBoxButton.OK, MessageBoxImage.Hand)
        End If
    End Sub

End Class


